<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!doctype html>
<html>
<meta http-equiv="Access-Control-Allow-Origin" content="*">
<jsp:include page="../common/import.jsp" flush="true"></jsp:include>


<body>
<jsp:include page="../common/header.jsp" flush="true"></jsp:include>


<div class="main p20">
	<div class="container clear">
        <ul class="left-nav fl pt20 w200">
        <c:if test="${user.roleId==0||user.roleId==1}">
	        <li><a href="${base}/agent/agent/agentList.xhtml"><span class="iconfont">&#xf014d;</span>代理商</a></li>
        </c:if>
            <!-- <li><a href="javascript:"><span class="iconfont">&#xf014d;</span>代理商</a></li> -->
            <li><a href="${base}/agent/sales/salesList.xhtml"><span class="iconfont">&#xf014d;</span>业务员</a></li>
            <li><a class="ahover" href="${base}/agent/merchant/merchantList.xhtml"><span class="iconfont">&#xf014b;</span>商户</a></li>
        </ul>
        <input type="hidden" name="userId" id="userId" value="${user.id}">
        <div class="main-right fr">
        	<div class="b m20 b-radius">
            	<div class="clear bgf5 bb">
                	<h2 class="fl h30 lg30 p5 pl10">开通新商户</h2>
                    <a href="javascript:" onclick="back()" class="fr h20 p5 w80 m5 bg1eb9ef cfff b-radius tc"><span class="vm">返回商户</span></a>
                </div>
                
                <div class="h450 p50 mt50">
                	<div class="clear">
                    	<div class="fl w300 tr lg30 p5 f16">手机号码：</div>
                        <input class="w300 p10 h20" type="text" id="phone">
                    </div>
                    <div class="clear mt20">
                    	<div class="fl w300 tr lg30 p5 f16">验证码：</div>
                        <input class="fl w150 p10 h20" type="text" id="captcha">
                        <button id="yzm" class="bga0 h20 p10 w100 ml30 cfff f16" onclick="getCaptcha()">获取验证码</button>
                    </div>
                    
                    <div class="tc mt50 cfff">
                    	<button class="bg1eb9ef p10 h20 w100 ml50" onclick="openMerchant()">开通</button>
                        <button class="bga0 p10 h20 w100 ml20" onclick="back()">取消</button>
                    </div>
                </div>
                
            </div>
        </div>
    </div>
</div>


<jsp:include page="../common/footer.jsp" flush="true"></jsp:include>

<script type="text/javascript">
//获取验证码
function getCaptcha(){
	var phone = $("#phone").val();
	if(phone.length==0)
    {
       alert('请输入手机号码！');
       return ;
    }    
    if(phone.length!=11)
    {
        alert('请输入11位有效的手机号码！');
        return ;
    }
    
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if(!myreg.test(phone))
    {
        alert('请输入有效的手机号码！');
        return ;
    }
	var url = "${base}/agent/merchant/captcha.xhtml";
	var data  = {"phone":phone};
	$.ajax({
		url: url,
		type: "POST",
		data: data,
		dataType: "JSON",
		success: function (output) {
			//alert(output);
			if(output.errCode==0){
				//倒计时60秒
				$('#yzm').attr('disabled','disabled').addClass('btn-default');
				var number1 = 59;
				var timer = setInterval(function(){
					$('#yzm').html(number1--);
					if(!number1){
						clearInterval(timer);
						$('#yzm').removeClass('btn-default').html('获取验证码');
						$('#yzm').attr('disabled','false');
					}
				},1000);
			}else{
	        	alert(output.msg);
			}
				
	        //location.reload(true);//刷新页面
        },
        error: function (data) {
            alert('服务端报错，' + output.errCode);
        }
	});
	
	
}
//开通商户
function openMerchant(){
	var userId = $("#userId").val();
	var phone = $("#phone").val();
	var captcha = $("#captcha").val();
	if (userId==null||userId=='') {
		alert("用户信息错误");
		return;
	}
	if (phone==null||phone=='') {
		alert("手机号不能为空");
		return;
	}
	if(phone.length!=11)
    {
        alert('请输入11位有效的手机号码！');
        return ;
    }
    
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if(!myreg.test(phone))
    {
        alert('请输入有效的手机号码！');
        return ;
    }
	if (captcha==null||captcha=='') {
		alert("验证码不能为空");
		return;
	}
	if (captcha.length!=4) {
		alert("请输入4位有效验证码");
		return;
	}
	
	var url = "${base}/agent/merchant/register.xhtml";
	var data  = {"userId":userId,"mobile":phone,"captcha":captcha};
	$.ajax({
		url: url,
		type: "POST",
		data: data,
		dataType: "JSON",
		success: function (output) {
        	alert(output.msg);
        	if (output.errCode==0) {
        		var qualificationId = output.data.qualificationId;
        		if (qualificationId&&qualificationId!='') {
	        		window.location.href="${base}/agent/merchant/toAddStorePage.xhtml?qualificationId="+qualificationId+"&phone="+phone;
				}
	       	 	//location.reload(true);//刷新页面
			}
        },
        error: function (output) {
            alert('服务端报错，' + output.errCode);
        }
	});
}
function back(){
	window.location.href = "${base}/agent/merchant/merchantList.xhtml";
}
</script>
</body>
</html>